const Koa = require('koa')
const router = require('koa-router')();//后端路由
const app = new Koa()
const community = require('./Data/community.json')
const exhibition = require('./Data/exhibition.json')
const cors = require('koa2-cors')//使用cors 来跨域
const Mock = require('mockjs')   //使用mockjs 来实现假数据
const Random = Mock.Random
const serve = require('koa-static');
app.use(serve(__dirname + '/public'));

const listcommunity = Mock.mock({
    'spot|20':[{
        'id':'@increment',//它会进行自增
        'name':'@ctitle(2,5)',//发布者的名字
        'title':'@ctitle(10,15)',//发布的专题
        'comments':'@ctitle(2,20)',//发表评论的内容
        'hupuaddress':'@ctitle(2,5)',//话题区域
        "number|1-100":100,//分享数
        "number2|1-100":100,//评论数
        "number3|1-100":100,//点赞数
        
        'imgsrc':Random.image('94x107')//头像
    }]
})

let listhome = Mock.mock({
    'list|20':[{
        'id':'@increment',//它会进行自增
        'name':'@ctitle(2,5)',//名字
        'title':'@ctitle(10,15)',//评论的话题
        'comments':'@ctitle(2,20)',
        'hupuaddress':'@ctitle(2,5)',//社区
        "number|1-100":100,//分享数
        "number2|1-100":100,//评论数
        "number3|1-100":100,//点赞数
        "number4|1-1000":1000,//阅读数
        'imgsrc':Random.image('94x107'),//头像
        "commentuser":"@ctitle(2,5)",//亮评的用户
        "commentlight":'@ctitle(2,80)',//亮评
        "commentimg":Random.image('94x107'),//亮评的图片
        //点入其中每一条的评论所需要的数据
        'evaluate|1-20':[{
            'imgsrc':Random.image('94x107'),//头像
            'name':'@ctitle(2,7)',//名字
            'comments':'@ctitle(2,20)',//评论的内容
            'light|1-2000':2000,
            'smile|1-2000':2000
        }]
    }]
})

let listshopping = Mock.mock({
    'list|20':[{
        "count":1,
        "isSelect":false,
        'id':'@increment',
        'title':'@ctitle(10,15)',
        'pricebefore|160-200':200,
        'price|90-159':159,
        'imgUrl1':Random.image('94X107')
    }]
})

//跨域
app.use(cors({
    origin: function(ctx) { //设置允许来自指定域名请求
        // if (ctx.url === '/test') {
        return '*'; // 允许来自所有域名请求
        // }
        // return 'http://localhost:3000'; //只允许http://localhost:8080这个域名的请求
    },
    maxAge: 5, //指定本次预检请求的有效期，单位为秒。
    credentials: true, //是否允许发送Cookie
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
}))
router.get('/home/home', async (ctx) => {
    // console.log(ctx,'/////');
    ctx.response.body = {
        success: true,
        data:listhome
    }
})


router.get('/home/shopping',async (ctx)=>{
    ctx.response.body = {
        success:true,
        data:listshopping
    }
})

router.get('/home/community', async (ctx) => {
    ctx.response.body = {
        success: true,
        // data:[...data,...community]
        data: {
            listcommunity,
            community
        }
    }
})

router.get('/detail/:id',async (ctx) =>{
    //  : 传的为params值，？传的为query的值
    const {id} = ctx.params
    if(!id){
        ctx.response.body = {
            success:false,
            msg:'请求有误'
        }
        return
    }
    const obj = listhome.list.find(item => item.id == id)
    //根据id 来寻找到 所需要的相应的值
    ctx.response.body ={
        success:true,
        data:obj
    }
})
router.get('/exhibition/:id', async (ctx)=>{
    const {id} = ctx.params
    if(!id){
        ctx.response.body={
            success:false,
            msg:'请求有误'
        }
        return
    }else if( id == 0){
        ctx.body = {
            success:true,
            data:exhibition.detailList[0]
        }
    }else if( id == 1){
        ctx.body = {
            success:true,
            data:exhibition.detailList[1]
        }
    }else if( id == 2){
        ctx.body = {
            success:true,
            data:exhibition.detailList[2]
        }
    }else if( id == 3){
        ctx.body = {
            success:true,
            data:exhibition.detailList[3]
        }
    }else{
        ctx.response.body={
            success:false,
            msg:'请求有误'
        }
        return
    }
})


app
    .use(router.routes())
    .use(router.allowedMethods())
    // 1. http服务
    // 2. 简单的路由模块
    // 3. cors
    // 4. 返回数据
app.listen(8080, () => {
    console.log('server is running 8080')
})
